<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="导购员管理">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <script>
        var dialogIndex;
        jQuery(function () {
            get("${baseUrl}organization/store/getStores.action",function (resp) {
                if (resp.code == 1) {
                    var $select = $("[name='searchStoreId']");
                    var $select1 = $("[name='storeId']");
                    $select.empty();
                    $select.append("<option value=''>全部</option>");
                    $select1.empty();
                    $.each(resp.data,function (i,data) {
                        $select.append("<option value='"+data.id+"'>"+data.name+"</option>");
                        $select1.append("<option value='"+data.id+"'>"+data.name+"</option>");
                    });
                    formRender();
                }
            });
        });

        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;


            form.on('submit(add)', function (data) {
                $("#handle").val("add");
                $("[lay-filter='shopGuide']")[0].reset();
                dialogIndex = openDialogByTagId("新增导购员", $("#shopGuideDialog"));
            });

            form.on('submit(save)', function (data) {
                if (data.field.name == "") {
                    tip("姓名不能为空");
                    return false;
                }
                console.log(JSON.stringify(data.field));
                var checkName = true;
                if (data.field.name == data.field.oldName) {
                    checkName = false;
                }

                delete data.field.oldName;
                post("${baseUrl}user/shopGuide/"+$("#handle").val()+".action?checkName="+checkName, data.field,function (resp) {
                    if (resp.code == 1) {
                        tip("保存成功");
                        setTimeout(function () {
                            layer.close(dialogIndex);

                            renderTable();
                            if ($("#handle").val() == 'update') {
                                $("[lay-filter='shopGuide']")[0].reset();
                                layui.form.render();
                            }
                        },1000);
                    } else {
                        tip(resp.msg);
                    }
                });

                return false;
            });

            form.on('submit(cancel)', function (data) {
                layer.close(dialogIndex);
                return false;
            });

            renderTable();

            form.on('submit(search)', function (data) {
                var json = data.field;
                table.reload('shopGuideTable',{
                    where: json
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });
        });

        var lineObj;
        function renderTable() {
            layui.table.render({
                id:'shopGuideTable',
                elem: '#shopGuideTable',
                url: "${baseUrl}user/shopGuide/getShopGuides.action?time="+(new Date()).valueOf(),
                cols: [[
                    {width:60,title:"序号",templet:function (d) {
                            return genTableIndex(d.status==2, "锁定", d);
                        }},
                    {field: 'id', width: 10, title: 'ID',hide:true},
                    {title: '操作', width: 100, templet: '#operBar'},
                    {field: 'name', width: 110, title: '姓名',style:"font-size:12px;"},
                    {field: 'store', width: 110, title: '所属门店',style:"font-size:12px;",templet:function (d) {
                        return d.store.name
                        }},
                    {field: 'remark', title: '备注',style:"font-size:12px;"}
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    //渲染完成，初始化操作框提示
                    initOperator();
                }
            });

            layui.table.on('tool(shopGuideTable)', function (obj) {
                lineObj = obj;
                //table中的数据
                var data = obj.data;
                if (obj.event === 'edit') {
                    data['oldName'] = data.name;
                    layui.form.val('shopGuide',data);
                    layui.form.render();
                    $("#handle").val("update");
                    dialogIndex = openDialogByTagId("编辑导购员", $("#shopGuideDialog"));
                } else if (obj.event === 'delete') {
                    layer.confirm('删除导购员《'+data.name+'》?', {icon: 3, title:'删除导购员'}, function(index){
                        post("${baseUrl}user/shopGuide/delete.action",{id:data.id}, function (resp) {
                            if (resp.code == 1){
                                tip("删除成功");
                                obj.del();
                                layer.close(index);
                            } else {
                                tip(resp.msg);
                            }
                        });
                    });
                }
            });
        }

    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="table-tool-div">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <a class="layui-btn" lay-submit="" lay-filter="add">新增</a>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">门店</label>
                        <div class="layui-input-inline">
                            <select name="searchStoreId">
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <input type="text" name="searchContent" id="searchContent" placeholder="搜索姓名" autocomplete="off" class="layui-input">
                    </div>

                    <div class="layui-inline">
                        <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                    </div>
                </div>
            </form>
        </div>

        <div class="horizon-line"></div>

        <table class="layui-hide" id="shopGuideTable" lay-filter="shopGuideTable"></table>

        <script type="text/html" id="operBar">
            <a lay-event="edit"><i class="layui-icon layui-icon-edit table-operator" tip="编辑"></i></a>
            <a lay-event="delete"><i class="layui-icon layui-icon-delete table-operator" tip="删除"></i></a>
        </script>
    </div>
</div>

</body>
<div id="shopGuideDialog" class="myDialog">
    <form class="layui-form" action="" lay-filter="shopGuide">
        <input type="hidden" id="handle">
        <input type="hidden" name="id">
        <input type="hidden" name="oldName">
        <input type="hidden" name="companyId">
        <div class="layui-form-item">
            <label class="layui-form-label">门店</label>
            <div class="layui-input-block">
                <select name="storeId" lay-verify="required">
                    <c:forEach var="store" items="${storeList}">
                        <option value="${store.id}">${store.name}</option>
                    </c:forEach>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" placeholder="姓名" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="备注" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常" checked>
                <input type="radio" name="status" value="2" title="锁定">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="save">保存</button>
                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="cancel">取消</button>
            </div>
        </div>
    </form>
</div>


</html>